using PKGames.AssetLoader;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace PKGames.AssetBundleSystem
{
	public class AssetBundleHelper
	{
		private const string Tag = "[AssetBundleHelper] ";

		private const string AssetBundlesOutputPath = "AssetBundles";

		public static Dictionary<string, int> ProcessAvailableAssetBundlesFiles()
		{
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
			if (MonoSingleton<AssetBundleManager>.Instance.IsEmbeddedBundles)
			{
				return dictionary;
			}
			string[] files = Directory.GetFiles(MonoSingleton<AssetBundleManager>.Instance.LocalAssetBundleBasePath);
			string[] array = files;
			foreach (string path in array)
			{
				try
				{
					string extension = Path.GetExtension(path);
					if (string.IsNullOrEmpty(extension))
					{
						string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
						string[] array2 = fileNameWithoutExtension.Split(new string[1]
						{
							"-"
						}, StringSplitOptions.None);
						if (array2.Length < 2)
						{
							try
							{
								File.Delete(path);
							}
							catch (Exception)
							{
								UnityEngine.Debug.Log("[MagicCameraAssetBundle] Removing legacy assetBundle files.");
							}
						}
						else
						{
							string[] array3 = fileNameWithoutExtension.Split(new string[1]
							{
								"_v"
							}, StringSplitOptions.None);
							if (array3.Length >= 2)
							{
								string key = array3[0];
								string s = array3[1];
								int result = 0;
								int.TryParse(s, out result);
								dictionary[key] = result;
							}
						}
					}
				}
				catch (Exception ex2)
				{
					UnityEngine.Debug.Log("[AssetBundleHelper] Cant resolve AssetBundleTable: " + ex2.Message);
				}
			}
			return dictionary;
		}

		public static string FindLatestDownloadedAssetBundle(string assetBundleName, int version, Dictionary<string, int> availableAssetBundleTable)
		{
			if (MonoSingleton<AssetBundleManager>.Instance.IsEmbeddedBundles)
			{
				return string.Empty;
			}
			if (!availableAssetBundleTable.ContainsKey(assetBundleName))
			{
				return string.Empty;
			}
			int num = availableAssetBundleTable[assetBundleName];
			UnityEngine.Debug.Log($"[MagicCameraAssetBundle] Latest Downloaded {assetBundleName} if {num.ToString()} >= {version.ToString()}");
			if (num >= version)
			{
				return $"{assetBundleName}_v{num.ToString()}";
			}
			string text = $"{MonoSingleton<AssetBundleManager>.Instance.LocalAssetBundleBasePath}{assetBundleName}_v{num.ToString()}";
			File.Delete(text);
			UnityEngine.Debug.Log("[MagicCameraAssetBundle] Removing obsolete assetBundle : " + text);
			return string.Empty;
		}

		public static void DownloadAssetBundle(string host, string assetBundle, Action<float> onSuccess, Action<float> onError, Action<float> onProgress)
		{
			string localPath = MonoSingleton<AssetBundleManager>.Instance.LocalAssetBundleBasePath + assetBundle;
			DownloadRequest downloadRequest = new DownloadRequest(localPath, host + assetBundle, 0L);
			DownloadListener listener = new DownloadListener(null, onSuccess, onError, onProgress);
			downloadRequest.AddListener(listener);
			MonoSingleton<AssetRequestManager>.Instance.DownloadQueue.Add(downloadRequest);
		}

		public static bool IsAssetBundleAvailable(string assetBundleName)
		{
			if (AssetBundleCache.IsAssetBundleCached(assetBundleName))
			{
				return true;
			}
			return false;
		}
	}
}
